package com.kehongborui.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Scanner;

public class LocalIpDemo {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getLocalHost();  // host 主机名 对象
//        System.out.println("主机名" + inetAddress.getHostName()); // 主机名
//        System.out.println("IP" + inetAddress.getHostAddress());  // 获取 本机 ip

        try {
            urlUri();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *
     */
    public static void urlUri() throws IOException {
//        URL url = new URL("http://www.importnew.com/871.html");  // 根据链接地址创建 url对象
        URL url = new URL("https://www.baidu.com/");  // 根据链接地址创建 url对象
        InputStream stream = url.openStream();  // 获取 url 网络流
        System.out.println(url.getDefaultPort());  // 获取远程服务器的端口

        // 缓冲流解析
//        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));  // 解析流
//
//        String line;
//
//        while ((line = reader.readLine()) != null) {
//            System.out.println(line);
//        }

        // 用 scanner 解析网络流
//        Scanner scanner = new Scanner(stream);
//        while (scanner.hasNext()) {
//            System.out.println(scanner.nextLine());
//        }
     }
}
